package cn.mymall.web.controller;

import cn.mymall.entity.Member;
import cn.mymall.redis.RedisHelper;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public class BaseController {
    @Autowired
    protected JdbcTemplate jdbcTemplate;
    @Autowired
    protected RedisHelper redisHelper;
    public Member getLoginMember() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        return (Member) request.getAttribute("login_member");
    }
    protected Map<String, Integer> filterPageInfo(Integer pageNo, Integer perPage) {
        Map<String, Integer> map = Maps.newHashMap();
        map.put("page_no", pageNo == null ? 1 : pageNo);
        perPage = perPage == null ? 10 : perPage;
        if (perPage > 100) {
            perPage = 100;
        }
        map.put("per", perPage);
        return map;
    }

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes holder = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (holder == null) return null;
        HttpServletRequest request = holder.getRequest();
        return request;
    }
}
